package com.ruoyi.teaching.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.YaStudent;
import com.ruoyi.teaching.domain.YaTeachingPlan;
import com.ruoyi.teaching.enums.DailyTaskEnum;
import com.ruoyi.teaching.mapper.YaStudentMapper;
import com.ruoyi.teaching.mapper.YaTeachingPlanMapper;
import com.ruoyi.teaching.vo.DailyInterviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaDailyTasksMapper;
import com.ruoyi.teaching.domain.YaDailyTasks;
import com.ruoyi.teaching.service.IYaDailyTasksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 每日任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-22
 */
@Slf4j
@Service
public class YaDailyTasksServiceImpl extends ServiceImpl<YaDailyTasksMapper,YaDailyTasks> implements IYaDailyTasksService
{
    @Autowired
    private YaDailyTasksMapper yaDailyTasksMapper;

    @Autowired
    private YaTeachingPlanMapper yaTeachingPlanMapper;

    @Autowired
    private YaStudentMapper yaStudentMapper;
    /**
     * 查询每日任务
     *
     * @param id 每日任务主键
     * @return 每日任务
     */
    @Override
    public YaDailyTasks selectYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.selectYaDailyTasksById(id);
    }

    /**
     * 查询每日任务列表
     *
     * @param yaDailyTasks 每日任务
     * @return 每日任务
     */
    @Override
    public List<YaDailyTasks> selectYaDailyTasksList(YaDailyTasks yaDailyTasks)
    {
        //只能查看学员自己的
        yaDailyTasks.setStudentId(SecurityUtils.getUserId());
        return yaDailyTasksMapper.selectYaDailyTasksList(yaDailyTasks);
    }

    /**
     * 新增每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int insertYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setCreateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.insertYaDailyTasks(yaDailyTasks);
    }

    /**
     * 修改每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int updateYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setUpdateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.updateYaDailyTasks(yaDailyTasks);
    }

    /**
     * 批量删除每日任务
     *
     * @param ids 需要删除的每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksByIds(Long[] ids)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksByIds(ids);
    }

    /**
     * 删除每日任务信息
     *
     * @param id 每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createDailyTask() {
        try {
            //1.先查询到每个班今日课表
            List<YaTeachingPlan> list = yaTeachingPlanMapper
                    .selectList(
                            new LambdaQueryWrapper<YaTeachingPlan>()
                                    .eq(YaTeachingPlan::getDate, DateUtils.getDate())
                    );

            //2.根据课表为每个班生成每日任务
            for (YaTeachingPlan yaTeachingPlan : list) {
                Long classesId = yaTeachingPlan.getClassesId();
                //根据班级查询到对应的学生
                List<YaStudent> studentList = yaStudentMapper.selectList(
                        new LambdaQueryWrapper<YaStudent>()
                                .eq(YaStudent::getClassesId, classesId)
                );
                for (YaStudent student : studentList) {
                    YaDailyTasks dailyTasks = new YaDailyTasks();
                    dailyTasks.setClassesId(classesId);
                    dailyTasks.setCourseContent(yaTeachingPlan.getCourseContent());
                    dailyTasks.setDate(yaTeachingPlan.getDate());
                    dailyTasks.setStudentId(student.getId());
                    dailyTasks.setStage(yaTeachingPlan.getStage());
                    dailyTasks.setStatus(DailyTaskEnum.UNSTARTED.code);
                    dailyTasks.setCreateBy("quartz");
                    dailyTasks.setCreateTime(new Date());
                    try {
                        yaDailyTasksMapper.insert(dailyTasks);
                    } catch (Exception e) {
                        log.warn("当前学员已创建每日任务{}",dailyTasks);
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    @Transactional(readOnly = true)
    public List<DailyInterviewVO> selectYaDailyTasksInterviewList(String date, Long classesId) {
        return Collections.emptyList();
    }
}
